<?php
namespace Cephalopod;

class LogRecorder
{
    private static bool $enableColor = false;

    private static bool $enableLog = true;

    public static function setEnableColor(bool $enableColor)
    {
        static::$enableColor = $enableColor;
    }

    public static function setEnableLog(bool $v)
    {
        static::$enableLog = $v;
    }

    public static function logError(string $message)
    {
        if (!static::$enableLog) return;
        $time = static::getTimeInfo();
        if (static::$enableColor) {
            fwrite(STDERR, "\033[1;31;40m[$time] [E] $message.\033[0m" . PHP_EOL);
        } else {
            fwrite(STDERR, "[$time] [E] $message." . PHP_EOL);
        }
    }

    public static function logInfo(string $message)
    {
        if (!static::$enableLog) return;
        $time = static::getTimeInfo();
        echo "[$time] [I] $message." . PHP_EOL;
    }

    private static function getTimeInfo(): string
    {
        $time = microtime(true);
        return date('Y-m-d H:i:s', intval($time)) . ' ' . sprintf('%03d', intval(1000 * ($time - intval($time))));
    }
}
